package com.yanqu.road.entity.task.args.union;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.entity.activity.union.RechargeUserInfo;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionRechargeUserArgs extends UnionActivityBaseArgs{

    private long userId;
    private int conditionId;
    private boolean getReward = false;

    private BigInteger setValue = BigInteger.valueOf(-1);

    public boolean isGetReward() {
        return getReward;
    }

    public void setGetReward(boolean getReward) {
        this.getReward = getReward;
    }

    public BigInteger getSetValue() {
        return setValue;
    }

    public void setSetValue(BigInteger setValue) {
        this.setValue = setValue;
    }

    public int getConditionId() {
        return conditionId;
    }

    public void setConditionId(int conditionId) {
        this.conditionId = conditionId;
    }

    public UnionRechargeUserArgs(int type, int addValue, String unionUid, long userId) {
        super(type,addValue,unionUid);
        this.userId = userId;
    }

    public long getUserId() {
        return userId;
    }

    @Override
    public BigInteger getFinalValue(String info){
        int count = 0;
        Map<Integer,List<RechargeUserInfo>> infoMap = JSON.parseObject(info, new TypeReference<Map<Integer, List<RechargeUserInfo>>>(){});
        if(!infoMap.containsKey(this.conditionId))infoMap.put(this.conditionId,new ArrayList<>());
        List<RechargeUserInfo> infoList = infoMap.get(conditionId);
        if(infoList == null){
            return BigInteger.ZERO;
        }
        for(RechargeUserInfo rechargeUserInfo : infoList){
            if (rechargeUserInfo.getTotalRecharge()>0){
                count++;
            }
        }
        return BigInteger.valueOf(count);
    }

    @Override
    public String addInfo(String info){
        //没有进行充值，不是领取奖励
        if(getAddValue() == 0 && !getReward)return info;
        boolean flag = false;
        //conditionId
        Map<Integer,List<RechargeUserInfo>> infoMap = JSON.parseObject(info, new TypeReference<Map<Integer, List<RechargeUserInfo>>>(){});
        if(infoMap == null)infoMap = new ConcurrentHashMap<>();
        if(!infoMap.containsKey(this.conditionId))infoMap.put(this.conditionId,new ArrayList<>());
        RechargeUserInfo rechargeUserInfo = new RechargeUserInfo();
        for(Integer conditionId : infoMap.keySet()){
            if(conditionId == this.conditionId){
                List<RechargeUserInfo> infoList = infoMap.get(conditionId);
                for(RechargeUserInfo data : infoList){
                    if(data.getUserId() == userId){
                        rechargeUserInfo = data;
                        flag = true;break;
                    }
                }
            }
            if(flag)break;
        }
        if(!flag){
            rechargeUserInfo.setUserId(userId);
            rechargeUserInfo.setTotalRecharge(0);
            rechargeUserInfo.setRechargeTime(0);
            rechargeUserInfo.setGetReward(false);
            infoMap.get(conditionId).add(rechargeUserInfo);
        }
        rechargeUserInfo.setTotalRecharge(rechargeUserInfo.getTotalRecharge()+getAddValue());
        if(getAddValue() != 0){
            rechargeUserInfo.setRechargeTime(rechargeUserInfo.getRechargeTime()+1);
        }
        if(getReward){
            rechargeUserInfo.setGetReward(getReward);
        }
        return JSON.toJSONString(infoMap);
    }
}
